///////////////////////////////////////////////////
// Election Administration Polarization
// Policy Simulation Plots
///////////////////////////////////////////////////

gl path "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"


// Simple model with two policies and control, LEOs target margin
qui forval x=50(5)95 {
	clear
	set obs 101
	local policy0_effect_dem = 1
	local policy0_effect_rep = 1
	local policy1_effect_dem = .`x'
	local policy1_effect_rep = 1
	local policy2_effect_dem = 1
	local policy2_effect_rep = .`x'
	gen pop_share_dem = (_n - 1)/100
	forval p=0/2 {
		gen votes_dem_policy`p' = `policy`p'_effect_dem'*pop_share_dem
		gen votes_rep_policy`p' = `policy`p'_effect_rep'*(1-pop_share_dem)
		gen margin_dem_policy`p' = votes_dem_policy`p' - votes_rep_policy`p'
	}
	egen margin_dem_leo_dem = rowmax(margin_dem_policy*)
	egen margin_dem_leo_rep = rowmin(margin_dem_policy*)
	gen margin_effect = margin_dem_leo_dem - margin_dem_leo_rep
	foreach g in dem rep {
		gen policy_`g' = 0 if margin_dem_leo_`g'==margin_dem_policy0
		replace policy_`g' = 1 if margin_dem_leo_`g'==margin_dem_policy1
		replace policy_`g' = 2 if margin_dem_leo_`g'==margin_dem_policy2
		gen vs_dem_leo_`g' = votes_dem_policy0/(votes_dem_policy0 + votes_rep_policy0) if policy_`g'==0
		replace vs_dem_leo_`g' = votes_dem_policy1/(votes_dem_policy1 + votes_rep_policy1) if policy_`g'==1
		replace vs_dem_leo_`g' = votes_dem_policy2/(votes_dem_policy2 + votes_rep_policy2) if policy_`g'==2
	}
	gen vs_effect = vs_dem_leo_dem - vs_dem_leo_rep
	gen vs_effect_ind = vs_dem_leo_dem - pop_share_dem
	gen p = `x'
	tempfile p`x'
	save `p`x''
}

// Plot the population share comparative static
use `p50', clear
qui forval x=55(5)95 {
	append using `p`x''
}
keep p pop_share_dem vs_effect vs_effect_ind
reshape wide vs_effect vs_effect_ind, i(pop_share) j(p)
twoway (connected vs_effect75 vs_effect90 vs_effect95 pop_share_dem, ///
		lc(gs10 gs6 gs2) mc(gs10 gs6 gs2)), ///
	ysc(r(0 0.15)) ylab(0(.05).15) ///
	xsc(r(0 1)) xlab(0(.25)1) ///
	legend(off) text(0.11 0.87 "Reduce Opponent" "Turnout by 25%" "p = 0.75") ///
	text(0.065 0.64 "Reduce Opponent" "Turnout by 10%" "p = 0.9") ///
	text(0.013 0.55 "Reduce Opponent" "Turnout by 5%" "p = 0.95") ///
	xti("Share Democrats in Population") ///
	yti("Effect of Dem Official on Dem Vote Share") ///
	graphregion(color(white))
graph export "$path/output/policy_simulation_plot.pdf", replace

